﻿using System;
using System.Runtime.CompilerServices;
using AzukiMap.Layer;
using AzukiMap.Map;
using AzukiMap.Tools.Logger;

namespace AzukiMap.Request
{
    public class PostGisRasterIndexRequest : PostGisVectorRequest
    {
        string _indexColumn;

        public string IndexColumn
        {
            get { return _indexColumn; }
            set { _indexColumn = value; }
        }


        public PostGisRasterIndexRequest(IMapStatus mapCanvas, IVectorLayer vectorLayer, string indexColumn)
            : base(mapCanvas, vectorLayer)
        {
            Logger.Debug(
                "PostGisRasterIndexRequest.PostGisRasterIndexRequest(2):"
                + " vectorLayer.Name = " + vectorLayer.Name
                );

            init(mapCanvas, vectorLayer);
        }

        public PostGisRasterIndexRequest(IMapStatus mapCanvas, IVectorLayer vectorLayer, string getColumnDataUri, string indexColumn)
            : base(mapCanvas, vectorLayer, getColumnDataUri)
        {
            Logger.Debug(
                "PostGisRasterIndexRequest.PostGisRasterIndexRequest(3):"
                + " vectorLayer.Name = " + vectorLayer.Name
                );

            init(mapCanvas, vectorLayer);
        }

        void init(IMapStatus mapCanvas, IVectorLayer vectorLayer, string indexColumn)
        {
            _indexColumn = indexColumn;
            init(mapCanvas, vectorLayer);
        }

        internal override void SetRequestParams()
        {
            SetMapParams();
            SetLayerParams();
            try
            {
                _params["table"] = _table.TableName;
                _params["column"] = _table.GeometryColumn;
                _params["tilecolumn"] = _indexColumn;
                _params["db"] = _table.DbName;
                _params["cr"] = _currentExtentWkt;
                _params["pr"] = _previousExtentWkt;
                _params["tolerance"] = _tolerance.ToString();
                _params["srid"] = _srid.ToString();
            }
            catch (ArgumentNullException ex)
            {
                Logger.Error("MapStatus or LayerStatus may be null.", ex);
            }
            catch (Exception ex)
            {
                Logger.Error("Something wrong to create a request params.", ex);
            }

            RequestParams = _params;

            Logger.Debug(
                "PostGisDataRequest.SetRequestParams(): "
                + " RequstParams = " + RequestParams.ToString()
                );
        }


    }
}
